﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace Department.admin.Membership
{
    public partial class ViewAllRoles : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Всички роли";

            AdminVariablesSingleton.changeCSS(Master, "admin");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Master, "viewRole");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");

            if (!IsPostBack)
            {
                itemsView.DataSource = Roles.GetAllRoles();
                itemsView.DataBind();
            }
        }

       

        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {

        }

        protected void toggleall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkHeader = (CheckBox)sender;
            for (int i = 0; i < itemsView.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)itemsView.Items[i].FindControl("CheckBoxIsMarked");
                chk.Checked = chkHeader.Checked;
            }
        }



        protected void itemsView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteARole")
            {
                Label tb = (Label)e.Item.FindControl("NameLabel");
                string deleteRole = tb.Text;
                Roles.DeleteRole(deleteRole);

            }
            
            if (e.CommandName == "Save")
            {
                TextBox tb = (TextBox)e.Item.FindControl("txtName");
                string createRole = tb.Text;
                try
                {
                    if (Roles.RoleExists(createRole))
                    {

                        return;
                    }

                    Roles.CreateRole(createRole);

                }
                catch (Exception exc)
                {
                    exc.ToString();
                }

            }
            itemsView.DataSource = Roles.GetAllRoles();
            itemsView.DataBind();
        }

    }
}